<?php
class IndexController extends Zendvn_Controller_Action{
	private $_paning = array('rowCount' => 20, 'pageRange' => 5);
	public function init(){
		parent::init();		
		$this->_templateObj->setPath(TEMPLATE_PATH . '/public/default');		
		$this->_templateObj->load();	
		
		/*====================================================
		 * Load Zendvn Translate for Controller
		 *====================================================*/
		$this->_langObj->setLangFile(array('validate.tmx'));		
		Zend_Registry::set('Zend_Translate',$this->_langObj->generate());
		
		/*====================================================
		 * Thiet lap gia tri cho phan phan trang cua controlle
		 *====================================================*/
		$pageNumber = $this->_request->getParam('page',1);
		$this->_paning['page'] = $pageNumber;
		$this->_arrParam['panigator'] = $this->_paning;
		$this->view->arrParam = $this->_arrParam;
	}
	
	public function indexAction(){
		$this->view->headLink()->appendStylesheet(TEMPLATE_URL . '/public/default/css/jquery.bxslider.css','screen');
		$this->view->headScript()->appendFile(TEMPLATE_URL . '/public/default/js/jquery.bxslider.js');
		
		$tblItem = new Zendvn_Model_Table_Product();
		$this->view->Items = $tblItem->listItem($this->_arrParam, array('task' => 'home-product'));
		
		$totalItem = $tblItem->totalItem($this->_arrParam);
			
		$panigator = Zendvn_Paginator::create($this->_arrParam['panigator'],$totalItem);
		$this->view->panigator = $panigator;
	}
	
	public function productCategoryAction(){
		/*==============================
		 * Category info
		*==============================*/
		$tblCategory = new Zendvn_Model_Table_ProductCategories();
		$this->view->categoryInfo = $tblCategory->getItem($this->_arrParam);
		
		
		$tblItem = new Zendvn_Model_Table_Product();
		$this->view->Items = $tblItem->listItem($this->_arrParam, array('task' => 'product-category'));

		$totalItem = $tblItem->totalItem($this->_arrParam);
			
		$panigator = Zendvn_Paginator::create($this->_arrParam['panigator'],$totalItem);
		$this->view->panigator = $panigator;
		
		/*========================================
		 * Set meta title - keywords - description
		 *========================================*/
		$title = Zendvn_Filter_Filter::filterContent($this->view->categoryInfo['name'], 'filter');
		$option['keywords'] = Zendvn_Filter_Filter::filterContent($this->view->categoryInfo['html_keyword'], 'filter');
		
		$html_description 	= Zendvn_Filter_Filter::filterContent($this->view->categoryInfo['html_descript'], 'filter');
		$article_content 	= Zendvn_Filter_Filter::filterContent(trim(strip_tags($this->view->categoryInfo['content'])), 'filter');
		$article_content 	= Zendvn_Filter_Filter::shortString($article_content, 200);
		
		$description		= empty($html_description)?$article_content:$html_description;
		
		$option['description'] = $description;
		$this->setTitle($title, $option);
	}
	
	public function productDetailAction(){
		$this->view->headScript()->appendFile(TEMPLATE_URL . '/public/default/js/jquery.prettyGallery.js');
		$tblItem 					= new Zendvn_Model_Table_Product();
		$this->view->Item 			= $tblItem->getItem($this->_arrParam, array('task' => 'info'));
	
		/*========================================
		 * Set meta title - keywords - description
		*========================================*/
		$title = Zendvn_Filter_Filter::filterContent($this->view->Item['name'], 'filter');
		$option['keywords'] = Zendvn_Filter_Filter::filterContent($this->view->Item['html_keyword'], 'filter');
	
		$html_description 	= Zendvn_Filter_Filter::filterContent($this->view->Item['html_descript'], 'filter');
		$article_content 	= Zendvn_Filter_Filter::filterContent(trim(strip_tags($this->view->Item['content'])), 'filter');
		$article_content 	= Zendvn_Filter_Filter::shortString($article_content, 200);
	
		$description		= empty($html_description)?$article_content:$html_description;
	
		$option['description'] = $description;
		$this->setTitle($title, $option);
		
		$arrShare[] = array('property', 'og:description', $description);
		$arrShare[] = array('property', 'og:title', $title);
		$arrShare[] = array('property', 'og:url', HTTP . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
		$arrShare[] = array('property', 'og:image', 'http://beboshop.vn/public/files/product/items/thumb1/alibaba_xi_trum_190.jpg');
		$this->setMeta($arrShare);
	}
	
	public function relatedProductAction(){
		if(!empty($this->_arrParam['product-id'])){
			$tblItem = new Zendvn_Model_Table_Product();
			$this->view->Items = $tblItem->listItem($this->_arrParam, array('task' => 'related-product'));
		}
		$this->_helper->layout->disableLayout();
	}
	
	public function tagsAction(){
		/*==============================
		 * Tag info
		*==============================*/
		$tblTag = new Zendvn_Model_Table_Tags();
		$this->view->tagInfo = $tblTag->getItem($this->_arrParam, array('task' => 'alias'));
		
		$view = (int)$this->view->tagInfo['view'] + 1;
		$tblTag->update(array('view' => $view), '`id` = ' . $this->view->tagInfo['id']);
		
		$this->_arrParam['tag_id'] = $this->view->tagInfo['id'];
		$tblItem = new Zendvn_Model_Table_Product();
		$this->view->Items = $tblItem->listItem($this->_arrParam, array('task' => 'product-tags'));
	
		$totalItem = $tblItem->totalItem($this->_arrParam);
			
		$panigator = Zendvn_Paginator::create($this->_arrParam['panigator'],$totalItem);
		$this->view->panigator = $panigator;
	
		/*========================================
		 * Set meta title - keywords - description
		*========================================*/
		$title = Zendvn_Filter_Filter::filterContent($this->view->tagInfo['name'], 'filter');
		$option['keywords'] = Zendvn_Filter_Filter::filterContent($this->view->tagInfo['html_keyword'], 'filter');
	
		$description 	= Zendvn_Filter_Filter::filterContent($this->view->tagInfo['html_descript'], 'filter');
	
		$option['description'] = $description;
		$this->setTitle($title, $option);
	}
	
	public function browseAction(){
		if(!empty($this->_arrParam['alias'])){
			$tblItem = new Zendvn_Model_Table_PageContent();
			$this->view->Item = $tblItem->getItem(array('page' => $this->_arrParam['alias']), array('task' => 'base-alias'));
	
			/*========================================
			 * Set meta title - keywords - description
			*========================================*/
			$title = Zendvn_Filter_Filter::filterContent($this->view->Item['name'], 'filter');
			$option['keywords'] = Zendvn_Filter_Filter::filterContent($this->view->Item['html_keyword'], 'filter');
			
			$html_description 	= Zendvn_Filter_Filter::filterContent($this->view->Item['html_descript'], 'filter');
			$article_content 	= Zendvn_Filter_Filter::filterContent(trim(strip_tags($this->view->Item['content'])), 'filter');
			$article_content 	= Zendvn_Filter_Filter::shortString($article_content, 305);
			$description		= empty($html_description)?$article_content:$html_description;
			$option['description'] = $description;
			
			$this->setTitle($title, $option);
		}
	}
	
	public function aboutUsAction(){
		$tblItem = new Zendvn_Model_Table_PageContent();
		$this->view->Item = $tblItem->getItem(array('page' => 'gioi-thieu'), array('task' => 'base-alias'));

		/*========================================
		 * Set meta title - keywords - description
		*========================================*/
		$title = Zendvn_Filter_Filter::filterContent($this->view->Item['name'], 'filter');
		$option['keywords'] = Zendvn_Filter_Filter::filterContent($this->view->Item['html_keyword'], 'filter');
			
		$html_description 	= Zendvn_Filter_Filter::filterContent($this->view->Item['html_descript'], 'filter');
		$article_content 	= Zendvn_Filter_Filter::filterContent(trim(strip_tags($this->view->Item['content'])), 'filter');
		$article_content 	= Zendvn_Filter_Filter::shortString($article_content, 305);
		$description		= empty($html_description)?$article_content:$html_description;
		$option['description'] = $description;
			
		$this->setTitle($title, $option);
		$this->_helper->viewRenderer('browse');
	}
	
	public function contactAction(){
	
		$this->view->headTitle('Liên hệ',true);
	
		$captcha = new Zendvn_Captcha_Image(array(
				'width' => 180,
				'height' => 54,
				'wordlen' => 4
		));
	
		$this->view->captcha = $captcha->render($this->view);
		$this->view->captcha_id = $captcha->getId();
	
		if($this->_request->isPost()){
			$validator = new Default_Form_ValidateContact($this->_arrParam);
			if($validator->isVaild()){
				$this->view->errors = $validator->getMessageErrors();
				$this->view->Item = $validator->getData();
			}else{
				$data = $validator->getData();
				$mail = new Zendvn_Mail();
				$result = $mail->sendContact($data);
				if($result){
					$this->_helper->flashMessenger->addMessage('Cảm ơn bạn đã gửi thông tin đến cho chúng tôi!');
				}else{
					$this->_helper->flashMessenger->addMessage('Error! Can not send!');
				}
				$this->_redirect($this->_currentController . '/contact');
			}
		}
		$this->view->messages = $this->_helper->flashMessenger->getMessages();
	}
	
	public function captchaImageAction(){
		$this->_helper->layout->disableLayout();
		$captcha = new Zendvn_Captcha_Image(array('width' => 180, 'height' => 54));
		$captchaID = $this->_request->getParam('captchaID',0);
		$captcha->removeCaptchaImg($captchaID);
	
		$this->view->captcha = $captcha->render($this->view);
		$this->view->captcha_id = $captcha->getId();
	}
	
	public function testAction(){
		 
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
	}
}